/**
 * Created by user on 2017/3/18.
 */
//案件里面的统计

 module.exports = function ($scope,$cookies,caseBarStatisticsService) {

   $scope.today = new Date();

   $scope.showWeek = showWeek;

  // 使用刚指定的配置项和数据显示图表。
   //新增任务
   $scope.newTaskInforX = [];
   $scope.newTaskInforY = [];

   //完成
   $scope.finishTaskInforX = [];
   $scope.finishTaskInforY = [];

   //项目成员--未完成任务
   $scope.resGroupUserUnFinishInforX = [];
   $scope.resGroupUserUnFinishInforY = [];

   //项目成员--完成任务
   $scope.resGroupUserFinishInforX = [];
   $scope.resGroupUserFinishInforY = [];

   var caseId = $cookies.get('caseAppId');
  //案件统计接口
   function caseStatistics() {
    var params = {'caseId':caseId};
    caseBarStatisticsService.caseStatistics(params).then(function (response) {
      if(response.data.code==1){
        //案件任务统计
        $scope.caseTaskStatistics = response.data.data.titleStatics;
        //新增任务
        $scope.newTaskInforX = response.data.data.resGroupDayNewX;
        $scope.newTaskInforY = response.data.data.resGroupDayNewY;
        //完成任务
        // $scope.finishTaskInforX = response.data.data.resGroupDayFinishX;
        $scope.finishTaskInforY = response.data.data.resGroupDayFinishY;
        // //项目成员--完成任务
        $scope.resGroupUserFinishInforX = response.data.data.resGroupUserFinishX;
        $scope.resGroupUserFinishInforY = response.data.data.resGroupUserFinishY;
        // //项目成员--未完成任务
        $scope.resGroupUserUnFinishInforX = response.data.data.resGroupUserUnFinishX;
        $scope.resGroupUserUnFinishInforY = response.data.data.resGroupUserUnFinishY;

        $scope.generalTaskList = "";
        $scope.generalTaskList = $scope.caseTaskStatistics[3].cnt;
        for(var i=0;i<$scope.caseTaskStatistics.length-1;i++){
          if($scope.caseTaskStatistics[i].flag=='未完任务'){
            $scope.notTaskList = ($scope.caseTaskStatistics[i].cnt/$scope.generalTaskList);
          }else if($scope.caseTaskStatistics[i].flag=='完成任务'){
            $scope.performTaskList = ($scope.caseTaskStatistics[i].cnt/$scope.generalTaskList);
          }else if($scope.caseTaskStatistics[i].flag=='延误任务'){
            $scope.delayTaskList =  ($scope.caseTaskStatistics[i].cnt/$scope.generalTaskList);
          }
        }

        //饼状图1
        // 指定图表的配置项和数据
        var Pie1_option = {
          tooltip: {
            trigger: 'item',
            backgroundColor:'#fafafa',
            textStyle:{
              color:['#ffb404','#0db1c0'],
              align:'left'
            },
            formatter: "{a} <br/>{b}: {c} ({d}%)"
          },
          series: [
            {
              name:'案件',
              type:'pie',
              radius: ['50%', '70%'],
              color:['#ffb304','#0eb1c1'],
              avoidLabelOverlap: false,
              label: {
                normal: {
                  show: false,
                  position: 'center'
                },
                emphasis: {
                  show: true,
                  textStyle: {
                    fontSize: '12',
                    // fontWeight: 'bold'
                  }
                }
              },
              labelLine: {
                normal: {
                  show: false
                }
              },
              data:[
                {value:$scope.performTaskList, name:'已完成'},
                {value:$scope.notTaskList, name:'未完成'}
                // {value:90, name:'已完成'},
                // {value:80, name:'未完成'}
              ]
            }
          ]
        };
        var myChartPie1 = echarts.init(document.getElementById('pie1'));
        myChartPie1.setOption(Pie1_option);
        //饼状图2
        var myChartPie2 = echarts.init(document.getElementById('pie2'));
        var Pie2_option = {
          tooltip: {
            trigger: 'item',
            backgroundColor:'#fafafa',
            textStyle:{
              color:['#f59245','#ccc'],
              align:'left'
            },
            formatter: "{a} <br/>{b}: {c} ({d}%)"
          },
          series: [
            {
              name:'案件',
              type:'pie',
              radius: ['50%', '70%'],
              color:['#f59245','#ccc'],
              avoidLabelOverlap: false,
              label: {
                normal: {
                  show: false,
                  position: 'center'
                },
                emphasis: {
                  show: true,
                  textStyle: {
                    fontSize: '12',
                    // fontWeight: 'bold'
                  }
                }
              },
              labelLine: {
                normal: {
                  show: false
                }
              },
              data:[
                {value:$scope.delayTaskList, name:'延误任务'},
                {value:$scope.generalTaskList, name:'总任务'}
              ]
            }
          ]
        };
        myChartPie2.setOption(Pie2_option);
        //折线
        var line_option = {
          title: {
            text: '每日新增和完成任务的趋势'
          },
          tooltip : {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
              label: {
                backgroundColor: '#afeae0'
              }
            }
          },
          legend: {
            data:['新增任务','完成任务']
          },
          color:['#2aa7ff','#b2e8dd','#54ceb6'],
          toolbox: {
            feature: {
              mark : {show: true},
              dataView : {show: true, readOnly: false},
              magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
              restore : {show: true},
              saveAsImage : {show: true}
            }
          },
          calculable : true,
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis : [
            {
              type : 'category',
              boundaryGap : false,
              splitLine: {show: false},
              axisLabel :{
                interval:0
              },
              min:0,
              // max:30,
              // data : ["2月8",'2月9','2月10','2月11','2月12','2月13','2月14','2月15','2月16','2月17','2月18','2月19','2月20','2月21','2月22','2月23','2月24','2月25','2月26'],
              data: $scope.newTaskInforX.map(function (str) {
                return str.replace(' ', '\n')
              })
            }
          ],
          yAxis : [
            {
              type : 'value',
              max:20,
            }
          ],
          series : [
            {
              name:'新增任务',
              type:'line',
              stack: '总量',
              areaStyle: {normal: {}},
              label: {
                normal: {
                  show: true,
                  position: 'bottom'
                }
              },
              // data:[22, 32, 12, 48, 30, 35, 36,46, 40,  19, 48, 10, 35, 50, 50, 29, 48, 24, 35, 9,46,40, 50, 39, 48, 15, 35, 40,23,]
              data:$scope.newTaskInforY
            },
            {
              name:'完成任务',
              type:'line',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'bottom'
                }
              },
              areaStyle: {
                normal: {}
              },
              // data:[12, 34, 23, 24,  30, 40,10, 50, 20, 70, 30, 40,10, 50, 20, 30, 40,10, 50, 20,30, 40,10, 50,30, 40,10, 50, 20]
              data:$scope.finishTaskInforY
            }
          ]
        };
        var myChartLine = echarts.init(document.getElementById('line'));
        myChartLine.setOption(line_option);
        //柱状
        var Bar_option = {
          title: {
            text: '项目成员'
          },
          tooltip : {
            trigger: 'axis',
            axisPointer : {            // 坐标轴指示器，坐标轴触发有效
              type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          color:['#2fb36f','#2aa7ff','#f48c37'],
          legend: {
            data:['已完成', '未完成']
          },
          // toolbox: {
          //   show : true,
          //   feature : {
          //     mark : {show: true},
          //     dataView : {show: true, readOnly: false},
          //     magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
          //     restore : {show: true},
          //     saveAsImage : {show: true}
          //   },
          // },
          calculable : true,
          xAxis : [
            {
              type : 'value',
              max:50,
              axisLabel :{
                interval:50
              },
              splitNumber: 5,
              minInterval: 1,
              interval:5,
              // data:$scope.resGroupUserFinishInforX
            }
          ],
          yAxis : [
            {
              type : 'category',
              max:5,
              // data : ['成员1','成员2','成员3','成员4','成员5','成员6','成员7','成员8','成员9','成员10']
              data :$scope.resGroupUserUnFinishInforX
            }
          ],
          series : [
            {
              name:'已完成',
              type:'bar',
              stack: '总量',
              itemStyle : { normal: {label : {show: true, position: 'insideRight'}}},
              // data:[320, 302, 301, 334, 390, 330, 320, 236, 364, 188]
              data:$scope.resGroupUserFinishInforY
            },
            {
              name:'未完成',
              type:'bar',
              stack: '总量',
              itemStyle : { normal: {label : {show: true, position: 'insideRight'}}},
              // data:[120, 132, 101, 134, 90, 230, 210, 156, 230, 132]
              // data :$scope.resGroupUserUnFinishInforX
              data : $scope.resGroupUserUnFinishInforY
            }
          ]
        };
        var myChartBar = echarts.init(document.getElementById('bar'));
        myChartBar.setOption(Bar_option);
      }
    })
  };

   function showWeek() {
     switch ($scope.today.getDay()){
       case 0: return  "星期日 ";
       case 1: return  "星期一 ";
       case 2: return  "星期二 ";
       case 3: return  "星期三 ";
       case 4: return  "星期四 ";
       case 5: return  "星期五 ";
       case 6: return  "星期六 ";
     }
   }
   caseStatistics();

};
